<template>
	<view>
		<scroll-view scroll-y 
		:style="{height:'calc(100vh - ' + height + 'rpx - env(safe-area-inset-bottom))'}">
			<slot name="title" :dataList="dataList" :searchData="searchData"></slot>
			<slot name="content" :dataList="dataList" :searchData="searchData"></slot>
			<uni-load-more :status="loadMoreStatus"></uni-load-more>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		name:"gz-list",
		props:{
			api: {
			  type:String,
			  required:true,
			  default:"",
			},
			height:{
				default:124,
				type:Number,
				required:true
			}
		},
		data() {
			return {
				searchData:{
				},
				loadMoreStatus:'noMore',
				dataList:[],
				apiMethod:"",
				config:{
					api:"",
					params:{},
					test:{
						isTest:false,
						list:[],
						filter:null,
					},
					success:null,
					fail:null
				}
			};
		},
		mounted() {
		},
		methods:{
			scrolltolower() {
				this.findAllByPage()
			},
			shuaxin(config) {
				this.config = config
				let api = (config && config.api)?config.api:""
				let params = (config && config.params)?config.params:{}
				let testData = (config && config.testData)?config.testData:{},
				
				//将参数添加到search
				if(params){
					uni.$common.obj2obj(params,this.searchData)
				}
				if(api){
					this.apiMethod = api
				}else{
					this.apiMethod = this.api
				}
				
				if(testData.isTest == true){
					this.dataList = testData.list
					if(testData.filter){
						this.dataList = this.dataList.filter(testData.filter)
					}
				}else{
					this.findAllByPage()
				}
			},
			findAllByPage(){
				if(!this.apiMethod){
					console.error("api为空")
					return
				}
				
				let apiMethod = uni.$api[this.apiMethod]
				if(!apiMethod){
					console.error("apiMethod为空")
					return
				}
				
				apiMethod(this.searchData).then((data)=>{
					let list = data.records
					list.forEach(item =>{
						this.dataList.push(item)
					})
					
					if(this.config.success){
						this.config.success({
							dataList:this.dataList,
							dataLength:this.dataList.length,
							isFinished: data.total == this.dataList.length, 
							total:data.total
						})
					}
				})
			},
			delByIndex(index){
				this.dataList.splice(index,1)
			},
			getByIndex(index){
				return this.dataList[index]
			},
			updateByIndex(index,item){
				this.dataList.splice(index,1,item)
			},
			getDataList(){
				return this.dataList
			},
			getDataLength(){
				if(!this.dataList){
					return 0
				}
				return this.dataList.length
			}
		}
	}
</script>

<style>
</style>
